using System.Text;
using ActiveSupport.Access;
using ActiveSupport.Conversions;

namespace ActiveSupport.Inflections
{
    public static class StringInflectionExtensions
    {
        public static string Capitalize(this string lowerCaseString)
        {
            return lowerCaseString.At(0).ToString().ToUpper() + lowerCaseString.From(1).ToLower();
        }

        public static string CapitalizeWords(this string lowerCaseString)
        {
            var result = new StringBuilder();
            
            foreach (var word in lowerCaseString.Split(' '))
            {
                result.AppendFormat("{0} ", word.Capitalize());
            }

            return result.ToString().TrimEnd(' ');
        }

        public static string Humanize(this string lowerCaseAndUnderscoredWord)
        {
            return lowerCaseAndUnderscoredWord.Capitalize().Replace('_', ' ');
        }

        public static string Pascalize(this string lowerCaseAndUnderscoredWord)
        {
            return lowerCaseAndUnderscoredWord.Replace('_', ' ').CapitalizeWords().Replace(" ", "");
        }

        public static string Camelize(this string lowerCaseAndUnderscoredWord)
        {
            // TODO: ok, this is getting out of hand...  :O
            return string.Concat(
                lowerCaseAndUnderscoredWord.Split('_').First(),
                lowerCaseAndUnderscoredWord
                    .Spacerize()
                    .CapitalizeWords()
                    .ToArray().Except(0)
                    .Join()
                );
        }

        public static string Dasherize(this string underscoredWord)
        {
            return underscoredWord.Replace('_', '-');
        }

        public static string Spacerize(this string underscoredWord)
        {
            return underscoredWord.Replace('_', ' ');
        }
    }
}